package com.tuozixuan.camel.exec;

import java.nio.charset.Charset;
import java.util.Objects;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.exec.ExecResult;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Component;


@Component
public class ExecDemoRoute extends RouteBuilder {

	@Override
	public void configure() {
		
//		from("timer:firedOnceTimer?repeatCount=1")
//    	.setBody(constant("Hello, Thread"))
//    	.threads(3, 10).threadName("MyThread").rejectedPolicy(ThreadPoolRejectedPolicy.Abort)
//        .to("log:threadDemoLog");
		
		/**
		from("timer:firedOnceTimer?repeatCount=1")
		//.to("exec:java?args=-version")
		.to("exec:java?args=-server")
		.process(new Processor() {

			@Override
			public void process(Exchange exchange) throws Exception {
				if (exchange.getIn().getBody() instanceof ExecResult) {
					ExecResult execResult = (ExecResult) exchange.getIn().getBody();
					String body = "";
					if (Objects.nonNull(execResult.getStderr())) {
						String stderr = IOUtils.toString(execResult.getStderr(), Charset.forName("GB2312"));
						body += "stderr:" + stderr;
					}
					if (Objects.nonNull(execResult.getStdout())) {
						String stdout = IOUtils.toString(execResult.getStdout(), Charset.defaultCharset());
						body += "stdout:" + stdout;
					}
					exchange.getIn().setBody(body);
				}
			}
		}).to("log:execDemoLog");
		*/
		
//		from("timer:firedOnceTimer?repeatCount=1").to("exec:cmd?args=/C echo echoString").process(new Processor() {
//
//			@Override
//			public void process(Exchange exchange) throws Exception {
//				if (exchange.getIn().getBody() instanceof ExecResult) {
//					ExecResult execResult = (ExecResult) exchange.getIn().getBody();
//					String body = "";
//					if (Objects.nonNull(execResult.getStderr())) {
//						String stderr = IOUtils.toString(execResult.getStderr(), Charset.forName("GB2312"));
//						body += "stderr:" + stderr;
//					}
//					if (Objects.nonNull(execResult.getStdout())) {
//						String stdout = IOUtils.toString(execResult.getStdout(), Charset.defaultCharset());
//						body += "stdout:" + stdout;
//					}
//					exchange.getIn().setBody(body);
//				}
//			}}).to("log:execDemoLog");;
		
	}

}
